<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="商品名称" prop="prodName">
        <el-input
          v-model="queryParams.prodName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入商家名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="兑换状态" prop="payStatus">
        <el-select v-model="queryParams.exchangeStatus" placeholder="请选择兑换状态" clearable size="small">
          <el-option label="未兑换" value="0"></el-option>
          <el-option label="已兑换" value="1"></el-option>
          <el-option label="已过期" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="兑换时间" prop="createdTime">
        <el-date-picker clearable size="small"
          v-model="dateRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          start-placeholder="开始时间"
          end-placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column prop="prod_id" label="商品ID" align="center" />
      <el-table-column prop="prod_name" label="商品名称" align="center" />
      <el-table-column prop="shop_name" label="商家名称" align="center" />
      <el-table-column prop="order_time" label="订单时间" align="center" />
      <el-table-column prop="username" label="用户名称" align="center" />
      <el-table-column prop="prod_score" label="所需积分" align="center" />
      <el-table-column prop="prod_count" label="商品数量" align="center" />
      <el-table-column prop="exchange_status" label="兑换状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.exchange_status === '0'">未兑换</span>
          <span v-else-if="scope.row.exchange_status === '1'">已兑换</span>
          <span v-else-if="scope.row.exchange_status === '2'">已过期</span>
          <span v-else>未知状态</span>
        </template>
      </el-table-column>
      <el-table-column prop="exchange_code" label="兑换码" align="center" />
      <el-table-column prop="exchange_time" label="兑换时间" align="center" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { productOrderList } from '@/api/union/order'

export default {
  name: 'productOrderList',
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: "a.order_time",
        isAsc: "desc",
        prodName: null,
        username: null,
        shopName: null,
        exchangeStatus: null
      },
      showSearch: true,
      dateRange: [],
      loading: true,
      list: [],
      total: 0
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      productOrderList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.loading = false
        this.total = response.total
        this.list = response.rows
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = []
      this.handleQuery();
    }
  }
}
</script>
